import { defineStore } from 'pinia'
import { getUserInfo } from '@/api/user'
import { logout } from '@/api/login'
import { getToken ,setToken, removeToken } from "@/stores/auth"

interface UserInfo {
	token : string | null
	info : Object | null
}

const useUserStore = defineStore('userStore', {
	state: () : UserInfo => {
		return {
			token: getToken(),
			info: null
		}
	},
    getters: {
        isLogin: (state) => !!state.token
    },
	actions: {
		async getUser() {
			const data = await getUserInfo();
            this.info = data;
		},
        login(token:string) {
            this.token = token;
            setToken(token);
        },
		async logout(is_call = true) {
			if(is_call && getToken())await logout();
            removeToken();
			this.token = null;
		}
	}
})

export default useUserStore